package com.q.tqb.activity.grab;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.HtmlActivity;
import com.q.tqb.activity.ViewPagerImageActivity;
import com.q.tqb.activity.mine.MineTweeterIntroActivity;
import com.q.tqb.adapter.RedCommentAdapter;
import com.q.tqb.adapter.RedRecordAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.GetPacketInfo;
import com.q.tqb.model.RedCommentInfo;
import com.q.tqb.model.RedDetailInfo;
import com.q.tqb.model.RedRecordInfo;
import com.q.tqb.model.ShareInfo;
import com.q.tqb.model.UserInfo;
import com.q.tqb.util.KeyboardUtils;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.MD5Util;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IRawResponse;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.util.oss.OssCallback;
import com.q.tqb.util.oss.OssService;
import com.q.tqb.view.SoundButton;
import com.q.tqb.view.popwindow.SharePopWindow;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


public class GrabSpeaking2Activity extends BaseActivity implements SoundButton.AudioFinishRecorderListener, View.OnClickListener {


    @BindView(R.id.et_comment_back)
    EditText mEtCommentBack;
    @BindView(R.id.tv_comment_back)
    TextView mTvCommentBack;
    @BindView(R.id.ll_comment_back)
    LinearLayout mLlCommentBack;

    private ImageView mIvHead;

    private TextView mTvName;

    private TextView mTvMoney;
    private TextView mTvAmount;

    private TextView mTvTro;
    private TextView tvPlayMoney;


    private SoundButton mBtPlay;


    @BindView(R.id.tv_get_money)
    TextView mTvGetMoney;

    @BindView(R.id.rv_comment)
    GRecyclerView mRvComment;

    @BindView(R.id.ll_detail)
    LinearLayout mLlDetail;

    @BindView(R.id.iv_open_money)
    ImageView mIvOpenMoney;

    @BindView(R.id.rl_open_red)
    RelativeLayout mRlOpenRed;

    @BindView(R.id.rl_red)
    RelativeLayout mRlRed;

    @BindView(R.id.iv_close)
    ImageView mIvClose;

    @BindView(R.id.iv_grab_share)
    ImageView mIvGrabShare;

    @BindView(R.id.ll_share)
    LinearLayout mLlShare;

    @BindView(R.id.ll_red_result)
    LinearLayout mLlRedResult;


    private String upPointName;
    private RedDetailInfo redDetailInfo;

    private ArrayList<RedRecordInfo> mRedRecordInfos;

    private ArrayList<RedCommentInfo> mRedCommentInfos;

    private RedCommentAdapter mRedCommentAdapter;
    private RedRecordAdapter mRecordAdapter;

    private int rid;
    private int ownUid;
    private int currentPageNo;
    private int commentType;
    private int isUserAgent;


    private boolean isCanFinish;
    private boolean isOpen;

    private String cMid;
    private String getMoney;
    private ShareInfo mShareInfo;

    private MyBroadcastReceiver mBroadcastReceiver;

    private AnimationDrawable animationDrawable;


    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView ivVip;
    private TextView tvSecret;
    private TextView tvUrl;
    private View viewLine;
    private String[] images;

    private boolean isComment;


    @Override
    public int getLayoutId() {
        return R.layout.activity_grab_speaking1;
    }

    @Override
    public void init(Bundle savedInstanceState) {

        rid = getIntent().getIntExtra("rid", 0);
        ownUid = (int) SPUtil.get(this, "uid", 0);
        isUserAgent = (int) SPUtil.get(this, "isUserAgent", 0);
        currentPageNo = 1;


        mRedRecordInfos = new ArrayList<>();
        mRedCommentInfos = new ArrayList<>();
        int type = getIntent().getIntExtra("type", 0);

        if (type == 1) {
            setTitle("地图红包");
        } else {
            setTitle("口令红包");
            setTvRight("分享");
        }

        //初始化头部信息
        initHeadView();

        //初始化微信回调广播
        initReceiver();

        //获得红包详情信息
        getRedDetailData();

        //获得红包记录第一页信息
        getRedRecordData();

        //获得红包评论信息
        getRedCommentData();

        //获得分享的信息内容
        getShareData();

    }

    /**
     * 初始化头部布局
     */
    private void initHeadView() {
        LinearLayout llGrabDetail = (LinearLayout) View.inflate(this, R.layout.view_grab_speaking_detail2, null);
        mIvHead = llGrabDetail.findViewById(R.id.iv_head);
        mTvName = llGrabDetail.findViewById(R.id.tv_name);
        mBtPlay = llGrabDetail.findViewById(R.id.bt_play);
        mTvMoney = llGrabDetail.findViewById(R.id.tv_money);
        mTvAmount = llGrabDetail.findViewById(R.id.tv_amount);
        mTvTro = llGrabDetail.findViewById(R.id.tv_tro);
        tvPlayMoney = llGrabDetail.findViewById(R.id.tv_play_money);
        viewLine = llGrabDetail.findViewById(R.id.view_line);


        tvSecret = llGrabDetail.findViewById(R.id.tv_secret);
        iv1 = llGrabDetail.findViewById(R.id.iv_1);
        iv2 = llGrabDetail.findViewById(R.id.iv_2);
        iv3 = llGrabDetail.findViewById(R.id.iv_3);
        ivVip = llGrabDetail.findViewById(R.id.iv_vip);

        RecyclerView rvRecord = llGrabDetail.findViewById(R.id.rv_record);
        LinearLayout mLlComment = llGrabDetail.findViewById(R.id.ll_comment);
        tvUrl = llGrabDetail.findViewById(R.id.tv_url);
        TextView tvMoreRecord = llGrabDetail.findViewById(R.id.tv_more_record);


        tvUrl.setOnClickListener(this);
        tvMoreRecord.setOnClickListener(this);
        mLlComment.setOnClickListener(this);
        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);


        mBtPlay.setAudioFinishRecorderListener(this);


        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(GrabSpeaking2Activity.this);
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvRecord.setLayoutManager(linearLayoutManager1);
        mRecordAdapter = new RedRecordAdapter(GrabSpeaking2Activity.this, mRedRecordInfos, true);
        rvRecord.setAdapter(mRecordAdapter);
        mRecordAdapter.setOnItemClickListener(new RedRecordAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                if (redDetailInfo == null) {
                    showToast("网络异常，请检查网络");
                } else {
                    Intent intent = new Intent(GrabSpeaking2Activity.this, RedRecordActivity.class);
                    intent.putExtra("headUrl", redDetailInfo.getHeader());
                    intent.putExtra("rid", redDetailInfo.getRid());
                    intent.putExtra("name", redDetailInfo.getNickname());
                    startActivity(intent);
                }
            }
        });

        mRedCommentAdapter = new RedCommentAdapter(GrabSpeaking2Activity.this, mRedCommentInfos);
        mRvComment.setLoadingMoreEnabled(true);
        mRvComment.setPullRefreshEnabled(false);
        mRvComment.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {}

            @Override
            public void onLoadMore() {
                currentPageNo++;
                getRedCommentData();
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(GrabSpeaking2Activity.this);
        mRvComment.requestDisallowInterceptTouchEvent(true);
        mRvComment.addHeaderView(llGrabDetail);
        mRvComment.setLayoutManager(linearLayoutManager);
        mRvComment.setAdapter(mRedCommentAdapter);


        mRedCommentAdapter.setOnItemClickListener(new RedCommentAdapter.OnItemClickListener() {
            @Override
            public void onBackClick(String mid) {
                cMid = mid;
                commentType = 2;
                mLlCommentBack.setVisibility(View.VISIBLE);
                isComment = true;
                KeyboardUtils.showSoftInput(GrabSpeaking2Activity.this);
                mEtCommentBack.setFocusable(true);
                mEtCommentBack.setFocusableInTouchMode(true);
                mEtCommentBack.requestFocus();
            }

            @Override
            public void onHeadClick(int redUid) {
                Intent intent = new Intent(GrabSpeaking2Activity.this, RedPersonDataActivity.class);
                intent.putExtra("uid", redUid);
                startActivity(intent);
            }
        });


    }


    /**
     * 评论红包
     */
    private void commentRed() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", ownUid + "");
        map.put("rid", rid + "");
        String comment = mEtCommentBack.getText().toString();
        if (commentType == 2) {
            map.put("p_cmid", cMid);
        }
        map.put("comment", comment);
        map.put("token", StringUtil.getToken(ownUid));
        //    LogUtil.i(map.toString());

        OkHttpUtil.getInstance().post(GrabSpeaking2Activity.this, AppConst.URL_GET_PACKET_COMMENT, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                KeyboardUtils.hideSoftInput(GrabSpeaking2Activity.this);
                showToast("评论成功");
                mEtCommentBack.setText("");
                currentPageNo = 1;
                getRedCommentData();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                //   LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
            }
        });
    }


    /**
     * 初始化微信分享回调广播
     */
    private void initReceiver() {
        mBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(AppConst.WX_SHARE);
        registerReceiver(mBroadcastReceiver, intentFilter);
    }


    /**
     * 请求红包详情数据
     */
    private void getRedDetailData() {
        mProgressDialog.show();
        Map<String, String> map = new HashMap<>();
        map.put("rid", rid + "");
        map.put("uid", ownUid + "");

        LogUtil.i(map.toString());
        OkHttpUtil.getInstance().post(GrabSpeaking2Activity.this, AppConst.URL_GET_PACKET_DETAIL, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                mProgressDialog.dismiss();
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                redDetailInfo = gson.fromJson(result, RedDetailInfo.class);

                Glide
                        .with(GrabSpeaking2Activity.this)
                        .load(redDetailInfo.getHeader())
                        .error(R.drawable.ic_head)
                        .dontAnimate()
                        .placeholder(R.drawable.ic_head)
                        .into(mIvHead);

                mIvHead.setOnClickListener(GrabSpeaking2Activity.this);

                if (TextUtils.isEmpty(redDetailInfo.getContent_link())) {
                    tvUrl.setVisibility(View.GONE);
                } else {
                    tvUrl.setVisibility(View.VISIBLE);
                }

                tvSecret.setText(redDetailInfo.getGet_secret());
                if (!TextUtils.isEmpty(redDetailInfo.getPacket_picture())) {
                    viewLine.setVisibility(View.VISIBLE);
                    images = redDetailInfo.getPacket_picture().split(";");
                    for (int i = 0; i < images.length; i++) {
                        if (i == 0) {
                            Glide
                                    .with(GrabSpeaking2Activity.this)
                                    .load(images[i])
                                    .into(iv1);
                        } else if (i == 1) {
                            Glide
                                    .with(GrabSpeaking2Activity.this)
                                    .load(images[i])
                                    .into(iv2);
                        } else if (i == 2) {
                            Glide
                                    .with(GrabSpeaking2Activity.this)
                                    .load(images[i])
                                    .into(iv3);
                        }
                    }
                }


                if (redDetailInfo.getIs_get() == 1) {
                    tvPlayMoney.setText(String.format("已领取该红包:%s元", redDetailInfo.getGet_money()));
                    tvPlayMoney.setVisibility(View.VISIBLE);
                    mBtPlay.setVisibility(View.GONE);
                } else if (redDetailInfo.getPacket_num() == redDetailInfo.getGet_num()) {
                    tvPlayMoney.setText("红包已领取完");
                    tvPlayMoney.setVisibility(View.VISIBLE);
                    mBtPlay.setVisibility(View.GONE);
                } else if (redDetailInfo.getCan_get() == 0) {
                    tvPlayMoney.setText(redDetailInfo.getNo_can_get_msg());
                    tvPlayMoney.setVisibility(View.VISIBLE);
                    mBtPlay.setVisibility(View.GONE);
//                    mLlShare.setVisibility(View.VISIBLE);
                    isCanFinish = true;
                } else {
                    tvPlayMoney.setVisibility(View.GONE);
                    mBtPlay.setVisibility(View.VISIBLE);
                }

                if (redDetailInfo.getPacket_level() == 2) {
                    if (isUserAgent != 1) {
//                        tvPlayMoney.setText("该红包为尊贵的推客专享");
//                        tvPlayMoney.setVisibility(View.VISIBLE);
                        mBtPlay.setVisibility(View.GONE);
//                        startTweeter();
                    }
                }

                if (redDetailInfo.getIs_users_agent() == 1) {
                    ivVip.setVisibility(View.VISIBLE);
                }

                if (TextUtils.isEmpty(redDetailInfo.getPacket_content())) {
                    mTvTro.setText(redDetailInfo.getPacket_title());
                } else {
                    mTvTro.setText(redDetailInfo.getPacket_content());
                }


                mTvName.setText(redDetailInfo.getNickname());
                mTvMoney.setText(String.format("共%s元", redDetailInfo.getPacket_money()));
                mTvAmount.setText(String.format("%d/%d个", redDetailInfo.getGet_num(), redDetailInfo.getPacket_num()));

            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {

                mProgressDialog.dismiss();
                //    LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
            }
        });
    }

    /**
     * 弹出框加入推客提示
     */
    private void startTweeter() {
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(GrabSpeaking2Activity.this);
        normalDialog.setTitle("温馨提示");
        normalDialog.setMessage("该红包为尊贵的推客专享，是否即刻加入推客");
        normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (isUserAgent == 0) {
                    Intent intent = new Intent(GrabSpeaking2Activity.this, MineTweeterIntroActivity.class);
                    startActivityForResult(intent, 52);
                }
                dialogInterface.dismiss();
            }
        });
        normalDialog.setNegativeButton("取消", null);
        normalDialog.show();
    }

    /**
     * 请求分享的标题和描述
     */
    private void getShareData() {
        OkHttpUtil.getInstance().post(GrabSpeaking2Activity.this, AppConst.URL_GET_SHARE_MSG, null, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                //    LogUtil.i(element.toString());
                Gson gson = new Gson();
                mShareInfo = gson.fromJson(element, ShareInfo.class);

            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                showToast(errorMsg);
            }
        });
    }

    //打开红包
    private void grabRed() {
        isOpen = true;
        animationDrawable = (AnimationDrawable) mIvOpenMoney.getDrawable();
        animationDrawable.start();
        Map<String, String> map = new HashMap<>();
        String addressCode = SPUtil.getString(GrabSpeaking2Activity.this, "addressCode");
        map.put("address", addressCode);
        map.put("rid", rid + "");
        map.put("uid", ownUid + "");
        map.put("token", StringUtil.getToken(ownUid));
        map.put("speech_address", AppConst.OSS_URL + upPointName);
        //  LogUtil.i(map.toString());
        OkHttpUtil.getInstance().post(GrabSpeaking2Activity.this, AppConst.URL_GET_PACKET, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                //  LogUtil.i(element.toString());


                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                GetPacketInfo getPacketInfo = gson.fromJson(result, GetPacketInfo.class);

                animationDrawable.stop();
                mIvOpenMoney.setImageResource(R.drawable.list_open);
                getMoney = getPacketInfo.data;

                grabRedSuccess();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                animationDrawable.stop();
                mRlRed.setVisibility(View.GONE);
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
            }
        });

    }


    /**
     * 抢红包成功
     */
    private void grabRedSuccess() {
        final double douGetMoney = StringUtil.halfUp(getMoney);
        mTvGetMoney.setText(String.format("%s元", douGetMoney));
        ScaleAnimation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(700);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mRlOpenRed.setVisibility(View.GONE);
                mLlRedResult.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mRlOpenRed.startAnimation(animation);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mLlRedResult != null) {
                    mLlRedResult.setVisibility(View.GONE);
                    mRlRed.setVisibility(View.GONE);
                    isCanFinish = false;
                    tvPlayMoney.setText(String.format("已领取该红包:%s元", douGetMoney));
                    tvPlayMoney.setVisibility(View.VISIBLE);
                    mBtPlay.setVisibility(View.GONE);
                    mTvAmount.setText(String.format("%d/%d个", redDetailInfo.getGet_num() + 1, redDetailInfo.getPacket_num()));
                    getRedCommentData();
                }
            }
        }, 1700);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }


    /**
     * 获得红包记录信息
     */
    private void getRedRecordData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", 1 + "");
        map.put("pageSize ", 10 + "");
        map.put("rid", rid + "");
        OkHttpUtil.getInstance().post(GrabSpeaking2Activity.this, AppConst.URL_GET_PACKET_DETAIL_LIST1, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                //   LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<RedRecordInfo> redRecordInfos = gson.fromJson(element, new TypeToken<ArrayList<RedRecordInfo>>() {
                }.getType());
                mRedRecordInfos.clear();
                mRedRecordInfos.addAll(redRecordInfos);
                mRecordAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
            }
        });
    }


    /**
     * 获得红包评论信息
     */
    private void getRedCommentData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", currentPageNo + "");
        map.put("pageSize ", 10 + "");
        map.put("rid", rid + "");
        OkHttpUtil.getInstance().post(GrabSpeaking2Activity.this, AppConst.URL_GET_PACKET_COMMENT_LIST, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                //   LogUtil.i(element.toString());
                if (mRvComment != null) {
                    Gson gson = new Gson();
                    ArrayList<RedCommentInfo> redCommentInfos = gson.fromJson(element, new TypeToken<ArrayList<RedCommentInfo>>() {
                    }.getType());
                    if (currentPageNo == 1) {
                        mRedCommentInfos.clear();
                    } else {
                        if (redCommentInfos.size() < 10) {
                            mRvComment.setNoMore(true);
                        } else {
                            mRvComment.loadMoreComplete();
                        }
                    }
                    mRedCommentInfos.addAll(redCommentInfos);
                    mRedCommentAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
            }
        });
    }


    @OnClick({R.id.iv_back, R.id.rl_open_red, R.id.iv_close, R.id.tv_right, R.id.iv_grab_share, R.id.tv_comment_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_comment_back:
                if (TextUtils.isEmpty(mEtCommentBack.getText().toString())) {
                    showToast("回复内容不能为空");
                } else {
                    mLlCommentBack.setVisibility(View.GONE);
                    isComment = false;
                    commentRed();
                }
                break;
            case R.id.iv_grab_share:
                isUserAgent = (int) SPUtil.get(GrabSpeaking2Activity.this, "isUserAgent", 0);
                if (isUserAgent == 0) {
                    Intent intent = new Intent(GrabSpeaking2Activity.this, MineTweeterIntroActivity.class);
                    startActivityForResult(intent, 52);
                }
                break;
            case R.id.rl_open_red:
                if (!isOpen) {
                    grabRed();
                }
                break;
            case R.id.iv_close:
                mLlShare.setVisibility(View.GONE);
                isCanFinish = false;
                break;
            case R.id.tv_right:
                share();
                break;
            default:
                break;
        }
    }


    /**
     * 分享
     */
    private void share() {
        if (mShareInfo == null || redDetailInfo == null) {
            showToast("网络异常，请检查网络");
            return;
        }
        mShareInfo.setUrl(AppConst.URL_SHARE + redDetailInfo.getRid());
        mShareInfo.setType(2);
        mShareInfo.setRedDetailInfo(redDetailInfo);
        SharePopWindow sharePopWindow = new SharePopWindow(GrabSpeaking2Activity.this, mShareInfo, null);
        sharePopWindow.showAtLocation(findViewById(R.id.rl_main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }


    @Override
    public void onBackPressed() {
        if (isComment) {
            mLlCommentBack.setVisibility(View.GONE);
            isComment = false;
        } else if (isCanFinish) {
            mRlRed.setVisibility(View.INVISIBLE);
            isCanFinish = false;
            mLlShare.setVisibility(View.INVISIBLE);
            isOpen = false;
        } else {
            super.onBackPressed();
        }
    }


    /**
     * 语音识别回调
     */
    @Override
    public void onFinished(String content) {
        if (redDetailInfo == null) {
            showToast("请检查网络！");
            return;
        }
        if (StringUtil.isMate(content, redDetailInfo.getGet_secret())) {
            mProgressDialog.show();
            String soundPath = StringUtil.SOUTH_PATH;
            upPointName = StringUtil.getUpSound(ownUid);
            OssService.asyncPutFile(upPointName, soundPath, new OssCallback(new Handler(), new IRawResponse() {
                @Override
                public void onSuccess(String response) {
                    mProgressDialog.dismiss();
                    isCanFinish = true;
                    mRlRed.setVisibility(View.VISIBLE);
                    mRlOpenRed.setVisibility(View.VISIBLE);
                    ScaleAnimation animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    animation.setDuration(500);
                    mRlOpenRed.startAnimation(animation);
                }

                @Override
                public void onFailure(String errorMsg) {
                    mProgressDialog.dismiss();
                    LogUtil.i(errorMsg);
                }
            }));
        } else {
            showToast("识别错误" + content);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 52 && resultCode == RESULT_OK) {
            isCanFinish = false;
            mLlShare.setVisibility(View.GONE);
            getRedDetailData();
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.ll_comment:
                mLlCommentBack.setVisibility(View.VISIBLE);
                isComment = true;
                KeyboardUtils.showSoftInput(GrabSpeaking2Activity.this);
                mEtCommentBack.setFocusable(true);
                mEtCommentBack.setFocusableInTouchMode(true);
                mEtCommentBack.requestFocus();
                commentType = 1;

                break;
            case R.id.tv_url:
                if (redDetailInfo != null) {
                    intent = new Intent(GrabSpeaking2Activity.this, HtmlActivity.class);
                    if (redDetailInfo.getContent_link().contains("http://") || redDetailInfo.getContent_link().contains("https://")) {
                        intent.putExtra("url", redDetailInfo.getContent_link());
                    } else {
                        intent.putExtra("url", "http://" + redDetailInfo.getContent_link());
                    }


                    startActivity(intent);
                }
                break;
            case R.id.tv_more_record:
                if (redDetailInfo == null) {
                    showToast("网络异常，请检查网络");
                } else {
                    intent = new Intent(GrabSpeaking2Activity.this, RedRecordActivity.class);
                    intent.putExtra("headUrl", redDetailInfo.getHeader());
                    intent.putExtra("rid", redDetailInfo.getRid());
                    intent.putExtra("name", redDetailInfo.getNickname());
                    startActivity(intent);
                }
                break;

            case R.id.iv_head:
                if (redDetailInfo == null) {
                    showToast("网络异常，请检查网络");
                } else {
                    intent = new Intent(GrabSpeaking2Activity.this, RedPersonDataActivity.class);
                    intent.putExtra("uid", redDetailInfo.getUid());
                    startActivity(intent);
                }
                break;
            case R.id.iv_1:
                startImageDetail(0);
                break;
            case R.id.iv_2:
                startImageDetail(1);
                break;
            case R.id.iv_3:
                startImageDetail(2);
                break;

        }


    }

    private void startImageDetail(int position) {
        Intent intent = new Intent(GrabSpeaking2Activity.this, ViewPagerImageActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("images", images);
        startActivity(intent);
    }


    private class MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Map<String, String> map = new HashMap<>();
            map.put("uid", ownUid + "");
            map.put("token", StringUtil.getToken(ownUid));
            OkHttpUtil.getInstance().post(GrabSpeaking2Activity.this, AppConst.URL_ADD_TIME_SHARE, map, new IResponse() {
                @Override
                public void onSuccess(JsonElement element) {
                    AlertDialog.Builder normalDialog = new AlertDialog.Builder(GrabSpeaking2Activity.this);
                    normalDialog.setTitle("温馨提示");
                    normalDialog.setMessage(R.string.grab_share_success);
                    normalDialog.setPositiveButton("确定", null);
                    normalDialog.show();
                    getRedDetailData();
                }

                @Override
                public void onFailure(int statusCode, String errorMsg) {
                    LogUtil.i(errorMsg + "statusCode" + statusCode);
                }
            });
        }
    }

}
